/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-09 15:54:27
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-12 09:48:30
 */
import { ApiResponse, ArticleDetail, Comment, CommentDetail } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'
export function getArticleInfo(id: string): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<ArticleDetail>>(`/articles/${id}`)
    // console.log('文章详情是', res.data.data)
    dispatch({
      type: 'articleDetail/setArticleInfo',
      payload: res.data.data
    })
  }
}

// 获取文章的评论
export function getCommentList(id: string, offset?: string): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<Comment>>('/comments', {
      params: {
        type: 'a',
        source: id,
        offset: offset
      },
    })
    // console.log(res, 17) // 输出结果，查看返回值的类型
    dispatch({
      type: 'article/saveComment',
      payload: res.data.data
    })
  }
}

export function likeAritcle(id: string, attitude: number): RootThunkAction {
  return async (dispatch) => {
    if (attitude === 1) {
      // 已经点赞需要==》取消点赞
      // 取消点赞
      await request.delete('/article/likings/' + id)
    } else {
      // 需要点赞
      // 点赞
      await request.post('/article/likings', { target: id })
    }
    // 刷新页面
    dispatch(getArticleInfo(id))
  }
}

export function collectArticle(id: string, is_collected: boolean): RootThunkAction {
  return async (dispatch) => {
    if (is_collected) {
      // 已经收藏需要==》取消收藏
      // 取消点赞
      await request.delete('/article/collections/' + id)
    } else {
      // 需要收藏
      //收藏
      // 收藏
      await request.post('/article/collections', {
        target: id,
      })
    }
    // 刷新页面
    dispatch(getArticleInfo(id))
  }
}

export function followUser(
  userId: number,
  is_follow: boolean,
  id: string
): RootThunkAction {
  return async (dispatch, getState) => {
    if (is_follow) {
      // 取消关注
      await request.delete('/user/followings/' + userId)
    } else {
      // 关注
      await request.post('/user/followings', {
        target: userId
      })
    }
    // 刷新页面
    // console.log(getState().articleDetail.info.art_id);

    // dispatch(getArticleInfo(id))
    await dispatch(getArticleInfo(getState().articleDetail.info.art_id))
  }
}

/* 给文章进行评论 */
export function addComment(
  target: string,
  content: string,
  articleId?: string | null,
): RootThunkAction {
  return async (dispatch, getState) => {
    /* 判断是对文章进行评论还是对评论进行回复 */
    const res = await request.post<ApiResponse<{ new_obj: CommentDetail }>>(
      '/comments',
      {
        target: target,  //articleId有值表示对评论进行回复 target=》com_id  否则 target=>articleId
        content,
        art_id: articleId || null
      }
    )
    // console.log(res.data.data.new_obj, 333)
    if (articleId) {
      // 对评论进行回复
      dispatch({
        type: 'article/saveNewReply',
        payload: res.data.data.new_obj
      })
    } else {
      // 对文章进行评论

      dispatch({
        type: 'article/saveNewComment',
        payload: res.data.data.new_obj
      })
    }

    // 刷新页面==获取评论列表
    // dispatch(getArticleInfo(articleId ? articleId : target))
    dispatch(getCommentList(articleId ? articleId : target, ''))
  }
}

// 获取评论的回复数据列表
// 获取评论的回复
export function getReplyList(id: string, offset: string): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<Comment>>('/comments', {
      params: {
        type: 'c',
        source: id, // 评论id
        offset,
      },
    })
    // console.log(res.data.data, 999)  // 'article/saveReply'
    dispatch({
      type: 'article/saveReply',
      payload: res.data.data
    })
  }
}